package com.blog.dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.blog.BO.AdminBO;
import com.blog.dao.AdminDao;
import com.blog.dao.BaseDao;
import com.blog.entity.Admin;

public class AdminDaoImpl implements AdminDao {
	@Override
	public Admin selectAdminByAdminNameAndAdminPass(String adminName, String adminPass) {
		// TODO Auto-generated method stub
		Connection con = null;
		PreparedStatement ps = null;
		ResultSet res = null;
		String sql = "SELECT * FROM admin WHERE adminName = ? AND adminPass = ?;";
		Admin admin = null;
		try {
			con = BaseDao.getCon();
			ps = con.prepareStatement(sql);
			ps.setString(1,adminName);
			ps.setString(2,adminPass);
			res = ps.executeQuery();
			if(res.next()) {
				admin = new Admin();
				admin.setAdminId(res.getInt("adminId"));
				admin.setAdminName(res.getString("adminName"));
				admin.setAdminPass(res.getString("adminPass"));
				admin.setAdminLevel(res.getInt("adminLevel"));
				SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
				Date dt = null;
				try {
					dt = format.parse(format.format(res.getTimestamp("regDate")));
				} catch (ParseException e) {
					// TODO Auto-generated catch block
					System.out.println("转换成Date的String不是规范值");
					e.printStackTrace();
				}
				admin.setRegDate(dt);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally {
			BaseDao.closeAll(res, ps, con);
		}
		return admin;
	}

	@Override
	public List<Admin> selectAdmin(AdminBO adminBO, int pageNum, int pageSize) {
		// TODO Auto-generated method stub
		Connection con = null;
		PreparedStatement ps = null;
		ResultSet res = null;
		List<Admin> adminList = new ArrayList<Admin>();
		List<Object>  params = new ArrayList<Object>();
		StringBuffer sql = new StringBuffer("SELECT * FROM admin where 1 = 1");
		if(adminBO != null) {
			//查询条件
			if(adminBO.getAdminId() != 0) {
				sql.append(" AND adminId = ?");
				params.add(adminBO.getAdminId());
			}//管理员id
			if(adminBO.getAdminName()!=null && !"".equals(adminBO.getAdminName())) {
				sql.append(" AND adminName LIKE concat('%',?,'%')");
				params.add(adminBO.getAdminName());
			}//管理员账户模糊查询
			if(adminBO.getAdminLevel() != 0) {
				sql.append(" AND adminLevel = ?");
				params.add(adminBO.getAdminLevel());
			}//管理员等级
			if(adminBO.getRegDate()!=null) {
				sql.append(" AND regDate = ?");
				params.add(adminBO.getRegDate());
			}//管理员注册时间
			if(adminBO.getRegStartDate()!=null) {
				sql.append(" AND regDate >= ?");
				params.add(adminBO.getRegStartDate());
			}//管理员从什么时间开始，包含输入的时间
			if(adminBO.getRegEndDate()!=null) {
				sql.append(" AND regDate <= ?");
				params.add(adminBO.getRegEndDate());
			}//管理员从什么时候结束，包含输入的时间
		}
		sql.append(" LIMIT ?,?");
		Admin selectAdmin = null;
		try {
			con = BaseDao.getCon();
			ps = con.prepareStatement(sql.toString());
			for(int i =0;i<params.size();i++) {
				ps.setObject(i+1, params.get(i));
			}
			ps.setInt(params.size()+1, (pageNum-1)*pageSize);
			ps.setInt(params.size()+2, pageSize);
			res = ps.executeQuery();
			while(res.next()) {
				selectAdmin = new Admin();
				selectAdmin.setAdminId(res.getInt("adminId"));
				selectAdmin.setAdminName(res.getString("adminName"));
				selectAdmin.setAdminPass(res.getString("adminPass"));
				selectAdmin.setAdminLevel(res.getInt("adminLevel"));
				SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
				Date dt = null;
				try {
					dt = format.parse(format.format(res.getTimestamp("regDate")));
				} catch (ParseException e) {
					// TODO Auto-generated catch block
					System.out.println("转换成Date的String不是规范值");
					e.printStackTrace();
				}
				selectAdmin.setRegDate(dt);
				adminList.add(selectAdmin);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally {
			BaseDao.closeAll(res, ps, con);
		}
		return adminList;
	}

	@Override
	public Admin selectAdminByAdminName(String adminName) {
		// TODO Auto-generated method stub
		Connection con = null;
		PreparedStatement ps = null;
		ResultSet res = null;
		String sql = "SELECT * FROM admin WHERE adminName = ?;";
		Admin admin = null;
		try {
			con = BaseDao.getCon();
			ps = con.prepareStatement(sql);
			ps.setString(1,adminName);
			res = ps.executeQuery();
			if(res.next()) {
				admin = new Admin();
				admin.setAdminId(res.getInt("adminId"));
				admin.setAdminName(res.getString("adminName"));
				admin.setAdminPass(res.getString("adminPass"));
				admin.setAdminLevel(res.getInt("adminLevel"));
				SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
				Date dt = null;
				try {
					dt = format.parse(format.format(res.getTimestamp("regDate")));
				} catch (ParseException e) {
					// TODO Auto-generated catch block
					System.out.println("转换成Date的String不是规范值");
					e.printStackTrace();
				}
				admin.setRegDate(dt);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally {
			BaseDao.closeAll(res, ps, con);
		}
		return admin;
	}

	@Override
	public int selectAdminNum() {
		// TODO Auto-generated method stub
		Connection con = null;
		PreparedStatement ps = null;
		ResultSet res = null;
		String sql = "SELECT COUNT(1) AS adminNum FROM admin;";
		int adminNum = 0;
		try {
			con = BaseDao.getCon();
			ps = con.prepareStatement(sql);
			res = ps.executeQuery();
			if(res.next()) {
				adminNum = res.getInt("adminNum");
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally {
			BaseDao.closeAll(res, ps, con);
		}
		return adminNum;
	}

	@Override
	public int insertAdmin(Admin admin) {
		// TODO Auto-generated method stub
		Connection con = null;
		PreparedStatement ps = null;
		String sql = "INSERT INTO admin(adminName,adminPass,adminLevel,regDate) VALUES (?,?,?,?);";
		int resultNum = 0;
		try {
			con = BaseDao.getCon();
			ps = con.prepareStatement(sql);
			ps.setString(1,admin.getAdminName());
			ps.setString(2,admin.getAdminPass());
			ps.setInt(3,admin.getAdminLevel());
			SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			String dt = format.format(admin.getRegDate().getTime());
			ps.setString(4,dt);
			resultNum = ps.executeUpdate();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally {
			BaseDao.closeAll(null, ps, con);
		}
		return resultNum;
	}

	@Override
	public int deleteAdminByAdminId(int adminId) {
		// TODO Auto-generated method stub
		Connection con = null;
		PreparedStatement ps = null;
		String sql = "DELETE FROM admin WHERE adminId = ?;";
		int resultNum = 0;
		try {
			con = BaseDao.getCon();
			ps = con.prepareStatement(sql);
			ps.setInt(1,adminId);
			resultNum = ps.executeUpdate();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally {
			BaseDao.closeAll(null, ps, con);
		}
		return resultNum;
	}

	@Override
	public int updateAdmin(Admin admin) {
		// TODO Auto-generated method stub
		List<Object>  params = new ArrayList<Object>();
		int resultNum = 0;
		if(admin.getAdminId() != 0) {
			Connection con = null;
			PreparedStatement ps = null;
			StringBuffer sql = new StringBuffer("UPDATE admin SET adminId = ?");
			params.add(admin.getAdminId());
			if(admin.getAdminName()!=null && !"".equals(admin.getAdminName())) {
				sql.append(",adminName = ?");
				params.add(admin.getAdminName());
			}//管理员账户名
			if(admin.getAdminPass()!=null && !"".equals(admin.getAdminPass())) {
				sql.append(",adminPass = ?");
				params.add(admin.getAdminPass());
			}//管理员密码
			if(admin.getAdminLevel() != 0) {
				sql.append(",adminLevel = ?");
				params.add(admin.getAdminLevel());
			}//管理员等级
			sql.append(",regDate = ?");
			sql.append(" WHERE adminId = ?");
			try {
				con = BaseDao.getCon();
				ps = con.prepareStatement(sql.toString());
				for(int i =0;i<params.size();i++) {
					ps.setObject(i+1, params.get(i));
				}
				SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
				String dt = format.format(new Date().getTime());
				ps.setString(params.size()+1,dt);
				ps.setInt(params.size()+2,admin.getAdminId());
				resultNum = ps.executeUpdate();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}finally {
				BaseDao.closeAll(null, ps, con);
			}
		}
		return resultNum;
	}

}
